package sqlinjection;

import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Atuan
 */
@ManagedBean(name = "movieConverter")
@SessionScoped
public class MovieConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        System.out.println("Converter: "+value);
        String editedValue = value.trim().split(" ")[0];
        System.out.println("EV: "+editedValue+";");
        List<Movie> movies = DBHandler.getAllMovies();
        Movie movieRes = new Movie();
        for(Movie movie : movies){
        System.out.println(""+editedValue+"-"+Long.toString(movie.getId()));
            if(editedValue.equals(Long.toString(movie.getId()))){
        System.out.println("yeah");
                movieRes = movie; 
            }
        }
        return movieRes;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Movie movie = (Movie)value;
//        System.out.print("EH "+movie.getId() +" - " + movie.getTitle());
        return Long.toString(movie.getId());
//        return movie.getId() +" - " + movie.getTitle();
    }
    
}
